<?php

namespace app\api\controller;

use app\api\validation\UserBankCard;
use app\services\UserBankCardService;
use support\Db;
use support\Request;

class UserBankCardController
{
    public function edit(Request $request)
    {
        try {
            $id = $request->post('id');
            $data = $request->post();

            $validate = new UserBankCard();
            if (!$validate->check($data)) {
                throw new \Exception($validate->getError());
            }

            UserBankCardService::edit($id, $data);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function add(Request $request)
    {
        try {
            $data = $request->post();

            $validate = new UserBankCard();

            if (!$validate->check($data)) {
                throw new \Exception($validate->getError());
            }

            UserBankCardService::add($data);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function delete(Request $request)
    {
        try {
            $id = $request->post('id');

            UserBankCardService::delete($id);

            return ok_rsp('成功');
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }

    public function list(Request $request)
    {
        try {
            $data = UserBankCardService::list();

            return ok_rsp('成功', $data);
        } catch (\Exception $e) {
            return err_rsp($e->getMessage());
        }
    }
}